﻿//

using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using MyWebPagesStarterKit.Rewriter;


namespace MyWebPagesStarterKit
{
    /// <summary>
    /// Example of using the rewrite 
    /// </summary>
    public class News : Section<News.Data>
    {
        public News(string id) : base(id) { }

        public News()
        {
            _data.Entries = new DataSet();
            _data.Entries.Tables.Add(new DataTable("Entries"));

            DataColumn primaryKeyColumn = new DataColumn("Guid", typeof(Guid));
            primaryKeyColumn.Unique = true;

            _data.Entries.Tables["Entries"].Columns.Add(primaryKeyColumn);
            _data.Entries.Tables["Entries"].PrimaryKey = new DataColumn[] { primaryKeyColumn };

            _data.Entries.Tables["Entries"].Columns.AddRange(
                new DataColumn[] 
                {
                    new DataColumn("Title", typeof(string)),
                    new DataColumn("Html", typeof(string)),
                    new DataColumn("StartDate", typeof(DateTime)),
                    new DataColumn("EndDate", typeof(DateTime)),
                    new DataColumn("RewriteUrl", typeof(string)), //Must be implemented, the url to rewrite.
                    new DataColumn("Keywords", typeof(string)),   //To be useful as a seo section these can be implemented
                    new DataColumn("Description", typeof(string)) // ------------------ '' -------------------------------

                }
           );
        }

        public override DataTable Entries
        {
            get { return _data.Entries.Tables["Entries"]; }
        }

        /// <summary>
        /// Returns a DataView with all news (no matter what values ShowUntil and/or NewsDate have)
        /// </summary>
        public DataView GetEntriesEdit()
        {
            return new DataView(Entries, string.Empty, "StartDate DESC", DataViewRowState.CurrentRows);
        }


        /// <summary>
        /// Returns the DataRow for the given Guid
        /// </summary>
        /// <param name="id">Guid of the record to return</param>
        public DataRow GetEditEntry(string id)
        {
            DataRow row = null;
            DataRow[] foundRows = Entries.Select(string.Format("Guid = '{0}'", id));
            if (foundRows.Length > 0)
                row = foundRows[0];
            return row;
        }

        /// <summary>
        /// Returns the DataRow for the given Guid
        /// </summary>
        /// <param name="id">Guid of the record to return</param>
        public DataRow GetEditEntryReadOnly(string id)
        {
            DataRow row = null;
            DataRow[] foundRows = Entries.Select(string.Format("Guid = '{0}'", id));
            if (foundRows.Length > 0)
                row = foundRows[0];
            return row;
        }


        /// <summary>
        /// Returns a DataView with all news that have a ShowUntil-Date in the future and NewsDate in the past
        /// </summary>
        public DataView GetEntriesReadonly()
        {
            string DateNow = DateTime.Today.AddDays(1).ToString("MM/dd/yyyy");
            string DateShowUntil = DateTime.Today.AddDays(0).ToString("MM/dd/yyyy");
            return new DataView(Entries, string.Format("StartDate <= #{0}# AND (EndDate >= #{1}# OR EndDate IS NULL)", DateNow, DateShowUntil), "StartDate DESC", DataViewRowState.CurrentRows);
        }


        public override List<SearchResult> Search(string searchString, WebPage page)
        {
            string DateShowUntil = DateTime.Today.AddDays(0).ToString("MM/dd/yyyy");
            string DateNow = DateTime.Today.AddDays(1).ToString("MM/dd/yyyy");
            List<SearchResult> foundResults = new List<SearchResult>();
            foreach (DataRow row in Entries.Rows)
            {
                row["Html"] = HttpUtility.HtmlDecode(SearchResult.RemoveHtml(row["Html"].ToString()));
            }
            DataRow[] foundRows = Entries.Select(string.Format("(Title LIKE '%{0}%' OR Html LIKE '%{0}%') AND EndDate >= #{2}# AND StartDate <= #{1}#", searchString.Replace("'", "''"), DateNow, DateShowUntil), "StartDate DESC");
            foreach (DataRow row in foundRows)
            {
                string url = string.Format("{0}{1}", SectionRewriter.GetPrefix("News"), Convert.ToString(row["RewriteUrl"]));

                foundResults.Add(
                    new SearchResult(
                        url,(string)row["Title"],
                        SearchResult.CreateExcerpt(SearchResult.RemoveHtml((string)row["Html"]), searchString)
                    )
                );
            }
            return foundResults;
        }

        public override bool IsOnline
        {
            get
            {
                return _data.IsOnline;
            }
            set
            {
                _data.IsOnline = value;
            }
        }

        public override Position Position
        {
            get
            {
                return _data.Position;
            }
            set
            {
                _data.Position = value;
            }
        }


        public override string PageID
        {
            get { return _data.PageID;  }
            set { _data.PageID = value; }
        }

        public class Data
        {
            public DataSet Entries;
            public bool IsOnline = false;
            public Position Position;
            public string PageID = string.Empty;
        }
    }
}
